<template>
  <!-- 导航条 -->
  <view class="content">
    <Navbar></Navbar>
    <scroll-view
      scroll-y
      @scrolltolower="scrolltolower"
      refresher-enabled
      @refresherrefresh="refresherrefresh"
      :refresher-triggered="refreshertriggered"
    >
      <Carousel :bannerList="bannerList"></Carousel>
      <CateScroll :categoryHeadMutli="categoryHeadMutli"></CateScroll>
      <!-- 热门推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hotMutli" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
            hover-class="none"
            class="more"
            url="/pages/recommend/index?type=5"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in homeNew"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <!-- 热门品牌 - 纯静态数据 -->
      <view class="panel brands">
        <view class="title">
          热门品牌
          <navigator hover-class="none" class="more" url="/pages/list/index"
            >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_1.jpg"
            ></image>
            <view class="name">小米</view>
            <view class="price">99元起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_2.jpg"
            ></image>
            <view class="name">TCL</view>
            <view class="price">199起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_3.jpg"
            ></image>
            <view class="name">饭小宝</view>
            <view class="price">9.9起</view>
          </navigator>
          <navigator hover-class="none" url="/pages/goods/index">
            <image
              mode="aspectFit"
              src="http://static.botue.com/erabbit/static/uploads/brand_logo_4.jpg"
            ></image>
            <view class="name">鳄鱼</view>
            <view class="price">299起</view>
          </navigator>
        </view>
      </view>
      <Guess :homeGoodsGuessLike="homeGoodsGuessLike" :counts="counts" />
    </scroll-view>
  </view>
</template>

<script>
import {
  getBannerListAPI,
  getCategoryHeadMutliListAPI,
  getHotMutliAPI,
  getHomeNewAPI,
  GoodsGuessLikeAPI
} from '@/api/home'
import Navbar from './components/Navbar.vue'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      bannerList: [],
      categoryHeadMutli: [],
      hotMutli: [],
      homeNew: [],
      homeGoodsGuessLike: [],
      counts: 0,
      refreshertriggered: false
    }
  },
  components: {
    Navbar
  },
  computed: {
    ...mapState(['safeArea', 'capsule'])
  },
  methods: {
    async getBannerList () {
      const { result } = await getBannerListAPI()
      this.bannerList = result
    },
    async getCategoryHeadMutliList () {
      const { result } = await getCategoryHeadMutliListAPI()
      this.categoryHeadMutli = result
    },
    async getHotMutli () {
      const { result } = await getHotMutliAPI()
      this.hotMutli = result
    },
    async getHomeNew () {
      const { result } = await getHomeNewAPI()
      this.homeNew = result
    },
    // 猜你喜欢
    async getGoodsGuessLike () {
      const { result } = await GoodsGuessLikeAPI(this.total)
      // this.homeGoodsGuessLike = result.items
      this.homeGoodsGuessLike = [...this.homeGoodsGuessLike, ...result.items]
      this.pages = result.pages
      this.counts = result.counts
      //获取总页数
    },
    scrolltolower (e) {
      if (this.loading) return
      // 如果猜你喜欢商品页小于后台返回的数据
      if (this.total.page < this.pages) {
        this.total.page++
        this.loading = true
        this.getGoodsGuessLike()
        this.loading = false
      }
    },
    // 下拉刷新触发事件
    refresherrefresh () {
      this.refreshertriggered = true

      // 初始化数据
      this.bannerList = []
      this.categoryHeadMutli = []
      this.hotMutli = []
      this.homeNew = []
      this.homeGoodsGuessLike = []
      this.counts = 0

      // 初始化cread事件函数
      this.loading = false
      this.total = { pageSize: 20, page: 1 }
      this.getBannerList()
      this.getCategoryHeadMutliList()
      this.getHotMutli()
      this.getHomeNew()
      this.getGoodsGuessLike()

      Promise.all([
        this.getBannerList,
        this.getCategoryHeadMutliList,
        this.getHotMutli,
        this.getHomeNew,
        this.getGoodsGuessLike
      ]).then(() => {
        this.refreshertriggered = false
        console.log(1)
      })
    }
  },
  created () {
    this.loading = false
    this.total = { pageSize: 20, page: 1 }
    this.getBannerList()
    this.getCategoryHeadMutliList()
    this.getHotMutli()
    this.getHomeNew()
    this.getGoodsGuessLike()
  }
}
</script>

<style lang="scss">
.page {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.navbar {
  background-image: url(http://static.botue.com/erabbit/static/images/navigator_bg.png);
  background-size: cover;
  position: relative;
  overflow: hidden;
  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;
    image {
      width: 166rpx;
      height: 39rpx;
    }
    text {
      flex: 1;
      line-height: 28rpx;
      color: #fff;
      margin: 2rpx 0 0 20rpx;
      padding-left: 20rpx;
      border-left: 1rpx solid #fff;
      font-size: 26rpx;
    }
  }
  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10rpx 0 26rpx;
    height: 64rpx;
    margin: 16rpx 20rpx;
    color: #fff;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: rgba(255, 255, 255, 0.5);
  }
  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }
  .icon-scan {
    font-size: 30rpx;
    padding: 15rpx;
  }
}
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
